package top.laoshuzi.rxmvp;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

public class Dialog extends DialogWrapper {

    private JTextField nameValue;
    private JTextField authorValue;

    OnClickListener clickListener = null;

    protected Dialog(@Nullable Project project) {
        super(project);
        setModal(true);
        setTitle("创建新的Rx-MVP类");
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {

        JLabel name = new JLabel("名称", JLabel.CENTER);

        nameValue = new JTextField();

        JLabel author = new JLabel("作者", JLabel.CENTER);

        authorValue = new JTextField();

        JPanel mainPanel = new JPanel();
        mainPanel.add(name);
        mainPanel.add(nameValue);
        mainPanel.add(author);
        mainPanel.add(authorValue);
        GridBagLayout l = new GridBagLayout();
        l.setConstraints(name, new GBC(0, 1, 1, 1));
        l.setConstraints(nameValue,
                new GBC(2, 1, 2, 1)
                        .setWeight(1, 0)
                        .setFill(GridBagConstraints.BOTH)
        );
        l.setConstraints(author, new GBC(0, 2, 1, 1));
        l.setConstraints(authorValue,
                new GBC(2, 2, 2, 1)
                        .setWeight(1, 0)
                        .setFill(GridBagConstraints.BOTH)
        );
//        mainPanel.setBorder(BorderFactory.createTitledBorder("命名"));
        mainPanel.setLayout(l);
        return mainPanel;
    }


    @Override
    protected void doOKAction() {
        super.doOKAction();
        if (clickListener != null) {
            clickListener.onOkClick();
        }
    }

    @Override
    public void doCancelAction() {
        super.doCancelAction();
        if (clickListener != null) {
            clickListener.onCancelClick();
        }
    }

    public String getName() {
        return nameValue.getText();
    }

    public String getAuthor() {
        return authorValue.getText();
    }

    public void setClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public interface OnClickListener {

        void onOkClick();

        void onCancelClick();

    }

}
